package com.ds.servlet;

import com.ds.model.AjaxPageResult;
import com.ds.model.AjaxResult;
import com.ds.model.BuildingInfo;
import com.ds.service.BuildingService;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/building")
public class BuildingServlet extends HttpServlet {
    private BuildingService buildingService= new BuildingService();

    /**
     *
     * @param req the {@link HttpServletRequest} object that contains the request the client made of the servlet
     *
     * @param resp the {@link HttpServletResponse} object that contains the response the servlet returns to the client
     *
     * @throws ServletException
     * @throws IOException
     */

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //String buildingId = req.getParameter("buildingId");
        String[] buildingIds = req.getParameterValues("buildingIds");

        AjaxResult result = buildingService.deleteBuilding(buildingIds);
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(result);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper om = new ObjectMapper();
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        AjaxPageResult result = buildingService.getBuildingList(Integer.parseInt(page),Integer.parseInt(limit));
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(om.writeValueAsString(result));
    }

    /**
     * 添加园区
     * @param req an {@link HttpServletRequest} object that contains the request the client has made of the servlet
     *
     * @param resp an {@link HttpServletResponse} object that contains the response the servlet sends to the client
     *
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper om = new ObjectMapper();
        BuildingInfo buildingInfo = om.readValue(req.getInputStream(), BuildingInfo.class);
        AjaxResult result = buildingService.addBuilding(buildingInfo);
        PrintWriter out = resp.getWriter();
        resp.setContentType("application/json;charset=utf-8");
        String resultJson = om.writeValueAsString(result);
        out.write(resultJson);

    }

    /**
     * 更新
     *
     * RESTful:
     * get: 查询
     * post: 添加
     * put: 更新
     * delete: 删除
     * @param req the {@link HttpServletRequest} object that contains the request the client made of the servlet
     *
     * @param resp the {@link HttpServletResponse} object that contains the response the servlet returns to the client
     *
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper om = new ObjectMapper();
        //获取前端传来的json数据
        BuildingInfo buildingInfo = om.readValue(req.getInputStream(), BuildingInfo.class);
        AjaxResult result = buildingService.updateBuilding(buildingInfo);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        String resultJson = om.writeValueAsString(result);
        out.write(resultJson);
    }
}
